public class SolutionInterview16_4 {
    public String tictactoe(String[] board) {
        for (String s : board) {
            boolean flag=true;
            for (int i=1;i<s.length();i++){
                if (s.charAt(i) != s.charAt(0)) {
                    flag = false;
                    break;
                }
            }
            if (flag && s.charAt(0)!=' '){
                return String.valueOf(s.charAt(0));
            }
        }
        for (int i=0;i<board.length;i++){
            boolean flag=true;
            for (int j=1;j<board.length;j++){
                if (board[j].charAt(i) != board[0].charAt(i)) {
                    flag = false;
                    break;
                }
            }
            if (flag && board[i].charAt(i)!=' '){
                return String.valueOf(board[i].charAt(i));
            }
        }
        boolean flag=true;
        for (int i=1;i<board.length;i++){
            if (board[i].charAt(i) != board[0].charAt(0)) {
                flag = false;
                break;
            }
        }
        if (flag && board[0].charAt(0)!=' '){
            return String.valueOf(board[0].charAt(0));
        }
        flag=true;
        for (int i=1;i<board.length;i++){
            if (board[i].charAt(board.length-i-1) != board[0].charAt(board.length-1)) {
                flag = false;
                break;
            }
        }
        if (flag && board[0].charAt(board.length-1)!=' '){
            return String.valueOf(board[0].charAt(board.length-1));
        }
        for (String s : board) {
            for (char c : s.toCharArray()) {
                if (c==' '){
                    return "Pending";
                }
            }
        }
        return "Draw";
    }

    public static void main(String[] args) {
        System.out.println(new SolutionInterview16_4().tictactoe(new String[]{"O X"," XO","X O"}));
    }
}
